 
#include "uiCommand.h"


uiCommand makeUICMD (   uint8_t _type,
                        uint16_t val0, 
                        uint16_t val1, 
                        uint16_t val2, 
                        uint16_t val3, 
                        uint16_t val4, 
                        uint16_t val5   ){
    uiCommand temp;
    temp.type = _type;
    temp.value[0] = val0;
    temp.value[1] = val1;
    temp.value[2] = val2;
    temp.value[3] = val3;
    temp.value[4] = val4;
    temp.value[5] = val5;
    return temp;
}


#ifdef PC
void printUICMD( uiCommand uic ){
    int i;
    
    printf(":: Type : %d \n:  ",uic.type);
    
    for (i = 0; i < 6; ++i){
        printf(" %d", uic.value[i]);
    }
    printf("\n");
    
}
#endif

int isUICMDSame( uiCommand uic1, uiCommand uic2 ){
    int retval = 1;
    int i;
    if (uic1.type == uic2.type) {
        for ( i = 0; i < 6; ++i){
            if (uic1.value[i] != uic2.value[i]){
                retval = 0;
            }
        }
    } else {
        retval = 0;
    }
      
    return retval;
}


bufferQue makeBQue ( uiCommand uiC){
    bufferQue retval;
    int val[6];
    
    val[0] = uiC.value[0];
    val[1] = uiC.value[1];
    val[2] = uiC.value[2];
    val[3] = uiC.value[3];
    val[4] = uiC.value[4];
    val[5] = uiC.value[5];
    
    bMakeEmpty(&retval);
    
    switch (uiC.type){
        case TEST_Q:
            bEnque(&retval, 'P');
            bEnque(&retval, 'T');
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case OK_A  :
            bEnque(&retval, 'O');
            bEnque(&retval, 'K');
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case NNETW_Q  :
            bEnque(&retval, 'P');
            bEnque(&retval, 'T');
            bEnque(&retval, 'S');
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case NNETW_A :
            bEnque(&retval, 'S');
            bEnque(&retval, ':');
            bEnque(&retval, val[0]);
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case SETPID_Q    :
            bEnque(&retval, 'K');
            bEnque(&retval, val[0]);
            bEnque(&retval, val[1]);
            bEnque(&retval, val[2]);
            bEnque(&retval, UICMDDELIMITER);        
        
        break;
        case SYST_ON  :
            bEnque(&retval, 'P');
            bEnque(&retval, 'T');
            bEnque(&retval, ' ');
            bEnque(&retval, 'O');
            bEnque(&retval, 'N');
            bEnque(&retval, UICMDDELIMITER);
        
        
        break;
        case SYST_OFF    :
            bEnque(&retval, 'P');
            bEnque(&retval, 'T');
            bEnque(&retval, ' ');
            bEnque(&retval, 'O');
            bEnque(&retval, 'F');
            bEnque(&retval, 'F');
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case NETW_Q  :
            bEnque(&retval, 'P');
            bEnque(&retval, 'T');
            bEnque(&retval, 'J');
            bEnque(&retval, val[0]);
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case NETW_A  :
            bEnque(&retval, 'N');
            bEnque(&retval, 'X');
            bEnque(&retval, ':');
            bEnque(&retval, val[0]);
            bEnque(&retval, (val[1] >> 8) & 0xFF );
            bEnque(&retval, val[1] & 0xFF);
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case PDATA_A  :
            bEnque(&retval, ':');
            bEnque(&retval, val[0]);
            bEnque(&retval, val[1]);
            bEnque(&retval, val[2]);
            bEnque(&retval, val[3]);
            bEnque(&retval, val[4]);
            bEnque(&retval, val[5]);
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case PDATA_Q  :
            bEnque(&retval, 'P');
            bEnque(&retval, 'X');
            bEnque(&retval, 'Y');
            bEnque(&retval, val[0]);
            bEnque(&retval, (val[1] >> 8) & 0xFF );
            bEnque(&retval, val[1] & 0xFF);
            bEnque(&retval, UICMDDELIMITER);
        
        break;
        case SETMODE_Q  :            
            bEnque(&retval, 'M');
            bEnque(&retval, 'S');
            bEnque(&retval, val[0]);
            bEnque(&retval, UICMDDELIMITER);
        break;
        case SETMODE_A   :
            bEnque(&retval, 'M');
            bEnque(&retval, ':');
            bEnque(&retval, val[0]);
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case GETNMODE_Q  :
            bEnque(&retval, 'P');
            bEnque(&retval, 'Z');
            bEnque(&retval, 'M');
            bEnque(&retval, UICMDDELIMITER);
            
        break;
        case GETNMODE_A  :
            bEnque(&retval, 'Z');
            bEnque(&retval, ':');
            bEnque(&retval, val[0]);
            bEnque(&retval, UICMDDELIMITER);
        break;
        
    }
    
    return retval;

}


uiCommand parseBQue ( bufferQue bQ){
    uiCommand retval;
    uint8_t temp[10];
    int i, commandLen;
    int itemp;
    
    commandLen = bLen(bQ);
    for (i = 0; i < commandLen; ++i){
        temp[i] = bDeque(&bQ);
    }
    
    retval = makeUICMD (0, 0, 0, 0, 0, 0, 0);
    if (commandLen > 0){
        if (temp[commandLen-1] == UICMDDELIMITER){
            if (commandLen == 3) {        
                if (temp[0] == 'P' && 
                    temp[1] == 'T'){
                    retval = makeUICMD (TEST_Q, 0, 0, 0, 0, 0, 0);
                    return retval;
                } else      
                if (temp[0] == 'O' && 
                    temp[1] == 'K'){
                    retval = makeUICMD (OK_A, 0, 0, 0, 0, 0, 0);
                    return retval;
                } 
                
                
            } else if (commandLen == 4) {
                if (temp[0] == 'P' && 
                    temp[1] == 'T' && 
                    temp[2] == 'S' ){
                        retval = makeUICMD (NNETW_Q, 0, 0, 0, 0, 0, 0);
                        return retval;            
                } else 
                if (temp[0] == 'M' && 
                    temp[1] == 'S'  ){
                        retval = makeUICMD (SETMODE_Q, temp[2], 0, 0, 0, 0, 0);
                        return retval;            
                } else 
                if (temp[0] == 'P' && 
                    temp[1] == 'Z' && 
                    temp[2] == 'M' ){
                        retval = makeUICMD (GETNMODE_Q, 0, 0, 0, 0, 0, 0);
                        return retval;            
                } else 
                if (temp[0] == 'S' && 
                    temp[1] == ':'){
                        retval = makeUICMD (NNETW_A, temp[2], 0, 0, 0, 0, 0);
                        return retval;            
                }else 
                if (temp[0] == 'M' && 
                    temp[1] == ':'){
                        retval = makeUICMD (SETMODE_A, temp[2], 0, 0, 0, 0, 0);
                        return retval;            
                }else 
                if (temp[0] == 'Z' && 
                    temp[1] == ':'){
                        retval = makeUICMD (GETNMODE_A, temp[2], 0, 0, 0, 0, 0);
                        return retval;            
                }
                
            } else if (commandLen == 5) {
                if (temp[0] == 'K'  ){
                        retval = makeUICMD (SETPID_Q, temp[1], temp[2], temp[3], 0, 0, 0);
                        return retval;            
                } else 
                if (temp[0] == 'P' && 
                    temp[1] == 'T' && 
                    temp[2] == 'J'){
                        retval = makeUICMD (NETW_Q, temp[3], 0, 0, 0, 0, 0);
                        return retval;            
                }
            
            } else if (commandLen == 6) {
                if (temp[0] == 'P' && 
                    temp[1] == 'T' && 
                    temp[2] == ' ' && 
                    temp[3] == 'O' && 
                    temp[4] == 'N'){
                        retval = makeUICMD (SYST_ON, 0, 0, 0, 0, 0, 0);
                        return retval;            
                }
            
            } else if (commandLen == 7) {
                if (temp[0] == 'P' && 
                    temp[1] == 'T' && 
                    temp[2] == ' ' && 
                    temp[3] == 'O' && 
                    temp[4] == 'F' && 
                    temp[5] == 'F'){
                        retval = makeUICMD (SYST_OFF, 0, 0, 0, 0, 0, 0);
                        return retval;            
                } else
                if (temp[0] == 'N' && 
                    temp[1] == 'X' && 
                    temp[2] == ':'){
                        itemp = (((temp[4] & 0xFF) << 8) | (temp[5] & 0xFF));
                        retval = makeUICMD (NETW_A, temp[3], itemp, 0, 0, 0, 0);
                        return retval;            
                } else 
                if (temp[0] == 'P' && 
                    temp[1] == 'X' && 
                    temp[2] == 'Y'){
                        itemp = (((temp[4] & 0xFF) << 8) | (temp[5] & 0xFF));
                        retval = makeUICMD (PDATA_Q, temp[3], itemp, 0, 0, 0, 0);
                        return retval;            
                }  
                
            } else if (commandLen == 8) {
                if (temp[0] == ':'){
                    retval =  makeUICMD( PDATA_A, 
                                         temp[1],
                                         temp[2],
                                         temp[3],
                                         temp[4],
                                         temp[5],
                                         temp[6]);
                    return retval;
                }
            }     
        }
    }
    
    return retval;
    
}



